<script>
import AddDoctorDialog from "@/components/comp/doctor_comm/AddDoctorDialog.vue";
  export default {
    name: "LoginView",
    components: {AddDoctorDialog},
    data(){
      return {
        login_form:{
          uname: '',
          pwd: '',
          role:2
        },
        rules: {
          uname: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
          ],
          pwd:[
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
          ]
        }
      }
    },
    methods:{
              login(){
                  //提交表单，验证数据是否合法
                 this.$refs.login_form_id.validate((valid) => {
                   if (valid) {
                     //提交表单的逻辑
                     this.subform();
                   } else {
                     this.$message.error("请填写正确的信息")
                   }
                 });
              },

            subform(){
                  const _this = this;
                  this.$http.post('accounts/login',this.login_form).then(function(res){
                      if(res.data.success){
                        const account1 = res.data.data;
                        //登陆成功
                        sessionStorage.setItem("uname",account1.uname);
                        sessionStorage.setItem("token",account1.token);
                        sessionStorage.setItem("role",account1.role);
                        sessionStorage.setItem("acc_id",account1.id);
                        sessionStorage.setItem("account",JSON.stringify(account1));
                        // 跳转到后台主页面
                        _this.$router.push({ path: '/home' });
                      }else{
                        //登陆失败
                        _this.$message.error(res.data.errMsg);
                      }
                  })
              },

              regist(){
                this.$refs.regist_doctor.show_dialog();
              },

              //注册成功能，把用户名回填到登陆界面
              regist_success(uname){
                        this.login_form.uname = uname;
                        this.login_form.pwd = "";
              }


    }
  }
</script>

<template>
  <div class="bj">
      <div class="login_container">
        <div class="img_container">
          <img src="../assets/logo.png">
        </div>

        <el-form ref="login_form_id" class="login_form" :model="login_form" :rules="rules">
          <el-form-item prop="uname">
            <el-input v-model="login_form.uname" prefix-icon="iconfont icon-yonghu" type="text"></el-input>
          </el-form-item>

          <el-form-item prop="pwd">
            <el-input v-model="login_form.pwd" prefix-icon="iconfont icon-mima"  type="password"></el-input>
          </el-form-item>



          <!-- <按钮区域 -->
          <el-form-item >
            <div class="btns" >
            <el-button type="primary"  @click="login">登陆</el-button>
            <el-button type="primary"  @click="regist">注册</el-button>
            </div>
          </el-form-item>



        </el-form>
      </div>
    <!--    注册操作员用户      -->
    <AddDoctorDialog ref="regist_doctor" @regist_ok= "regist_success"></AddDoctorDialog>
  </div>
</template>


<style scoped>
  .bj{
      width: 100%;
      height: 100%;
      background-image: url("../assets/imgs/log_bj.jpg");
      background-size: cover;
      background-position: center;
  }
  .login_container{
    width:350px;
    height:350px;
    //background-color: #C8E7FB;
    position:absolute;
    top:50%;
    left:50%;
    transform: translate(-50%,-50%);
  }

  .img_container{
    width:130px;
    height: 130px;
    background-color: #C8E7FB;
    border-radius: 50%;
    padding: 20px;
    position: absolute;
    left: 50%;
    transform: translate(-50%,-50%);
    border:1px solid rgba(241, 231, 231, 0.192);
  }

.img_container img{
  width:130px;
  height: 130px;
  border-radius: 50%;
  background-color:#C8E7FB;
}

.btns{
  //background-color: red;
  display: flex;
  justify-content:space-around;
}

.login_form{
  position: absolute;
  bottom:0px;
  width:100%;
  padding: 20px;
  box-sizing: border-box;
}

</style>


